package com.frame.work.frame.deal.device0fc8;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.frame.deal.base.BaseFrame;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static java.lang.Float.parseFloat;

/**
 *计量插座16A
 */
@Data
public class JiLiangChaZuo_16A_0FC80403 extends BaseFrame {

    private String switchState;

    public JiLiangChaZuo_16A_0FC80403(String gatewayMac, String data){
        super(gatewayMac,data);
        // 旧版
        if("2D".equals(StringUtils.substring(data,2, 4))) {
            data = StringUtils.substring(data,4, data.length() - 12) + StringUtils.substring(data,data.length() - 6, data.length() - 4);
        } else { // 新版
            data = StringUtils.substring(data,4, data.length() - 4);
        }


        //将基础数据进行处理
        this.setGatewayMac(gatewayMac);
        this.setDeviceMac(StringUtils.substring(data, 2, 18));
        this.setOdIndex(StringUtils.substring(data, 18, 22));
        this.setDeviceType(StringUtils.substring(data, 34, 36));
        this.setProductType(StringUtils.substring(data, 36, 38));





        //电压
        String dianya = StringUtils.substring(data,data.length() - 26, data.length() - 22);
        dianya = StringUtils.substring(dianya,2, 4) + StringUtils.substring(dianya,0, 2);
        dianya = StringUtils.substring(dianya,0, 3) + '.' + StringUtils.substring(dianya,3, 4);
        dianya = Float.toString(parseFloat( dianya));
        //电流
        String dianliu = StringUtils.substring(data,data.length() - 22, data.length() - 16);
        dianliu = StringUtils.substring(dianliu,4, 6) + StringUtils.substring(dianliu,2, 4)+ StringUtils.substring(dianliu,0, 2);
        dianliu = StringUtils.substring(dianliu,0, 3) + '.' + StringUtils.substring(dianliu,3, 6);
        dianliu = Float.toString(parseFloat( dianliu));
        //功率
        String gonglv =StringUtils.substring(data,data.length() - 16, data.length() - 10);
        gonglv = StringUtils.substring(gonglv,4, 6) + StringUtils.substring(gonglv,2, 4) + StringUtils.substring(gonglv,0, 2);
        gonglv = StringUtils.substring(gonglv,0, 5) + '.' + StringUtils.substring(gonglv,5, 6);
        gonglv = Float.toString(parseFloat( gonglv));
        //电量
        String dianliang = StringUtils.substring(data,data.length() - 10, data.length() - 2);
        dianliang = StringUtils.substring(dianliang,6, 8) + StringUtils.substring(dianliang,4, 6)+ StringUtils.substring(dianliang,2, 4)+ StringUtils.substring(dianliang,0, 2);
        dianliang = StringUtils.substring(dianliang,0, 6) + '.' + StringUtils.substring(dianliang,6, 8);
        dianliang = Float.toString(parseFloat( dianliang));
        this.setSwitchState(StringUtils.substring(data, data.length()-2, data.length()));
        //将actions信息放入json对象中
        Map <String,Object> actions = new HashMap();
        actions.put("dianya",dianya);
        actions.put("dianliu",dianliu);
        actions.put("gonglv",gonglv);
        actions.put("dianliang",dianliang);
        actions.put("switchState",switchState);
        this.setActions(actions);


        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac,json);
    }

}
